package org.yray.somehttp.http;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Zhidao {

    public  final static String zhidao="https://zhidao.baidu.com/search?word=";
    public static void main(String[] args) throws Exception {
        //从URL加载HTML
        Map<String,String> header=new HashMap<>();
        header.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
        header.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        header.put("Accept-Encoding","gzip, deflate, br");
        header.put("Accept-Language","zh-CN,zh;q=0.9");
        header.put("Cookie","BDUSS=ptaUgtYlJXRFRqVVdBYlM4RkdDSExVTzV5czgzakhXR0lBWU9CRVdYcXN6TUZnSVFBQUFBJCQAAAAAAAAAAAEAAABZiY8BaGlndWFuZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKw~mmCsP5pgbW; BDUSS_BFESS=ptaUgtYlJXRFRqVVdBYlM4RkdDSExVTzV5czgzakhXR0lBWU9CRVdYcXN6TUZnSVFBQUFBJCQAAAAAAAAAAAEAAABZiY8BaGlndWFuZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKw~mmCsP5pgbW; BIDUPSID=5A895D9E70638A6EF39A348281777E55; PSTM=1620722960; __yjs_duid=1_27d6b3de48808e4ffd614b88ea1a6da61620801205698; BAIDUID=426C0AF46175E08C9432CA18907775F0:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDSFRCVID_BFESS=b5DOJeC624zWDGJD5wvqboBG21l_wYrTH6aoWp3m0r_JjNCTvdo8EG0PKU8g0K4bwmZLogKK3gOTH4PF_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tJK8VIL2JC_3fP36q4jHhRD0-fIX5-RLf2ueal7F5l8-hx_RLp-hXU30QJnm5tRG3brMLK38Q4oxOKQIDnJRW-Kl5loD0pvtL6c9Vx7N3KJmMqC9bT3v5tjWLGrz2-biWb7M2MbdJUJP_IoG2Mn8M4bb3qOpBtQmJeTxoUJ25DnJhbLGe6-2j6oyjauDqbbfb-oX3bnbabQMfnj4bDTjhPrM-GoRWMT-0bFH_Pc63p3ksK565RQDhUD1DGQQybbk-Hn7_JjOQ-t5DDJ4BU68Xxt75xOyqUQxtNRi-CnjtpkMH43zWxvobUPUDUJ9LUkJLgcdot5yBbc8eIna5hjkbfJBQttjQn3hfIkj0DKLK-oj-DKljjA23e; H_PS_PSSID=35837_35970_35104_31660_36005_35911_34584_36001_35871_35945_35994_35956_35982_35320_26350_35882_35878_36009; BA_HECTOR=alaha0000085a0a0411h2hfs20r; Hm_lvt_6859ce5aaf00fb00387e6434e4fcc925=1646549436,1646646526,1646740345,1646837637; shitong_key_id=2; session_id=16468382549215387361377260458; ZD_ENTRY=empty; Hm_lpvt_6859ce5aaf00fb00387e6434e4fcc925=1646838287; ab_sr=1.0.1_ZjE5OGVkNzg1MGUwZjQyM2M1YmNkNDQ3MGM3MzMxYmE4NjYxNWNkZDFkMzA0ZDgyOWE2YTMwZmM5OWMxMDA1OTJkYWVlYTNmZTg2NGI2MzJhOGZmNmNlNWM2Y2M1YzdkYzUwYmJjNGU3MDQ0YWVlYjY1YzBmZmQyYjg5OGI3ZWJmNjRmNDFlYTQ2MDhkMmZkYmNhM2JkZDliNGU3MWI2N2UyNDU0Y2YyZWQ1MTE0MWYxMmYyZjk4MmVjY2FiOWJh; shitong_data=eebdb35a05bfbe1f16608c96ae316c5e1dfd9b413ce6c2a47a3deb8c668cc4ae8778bd7dc2f2f97780dcdc1c79ef1213c52aa8d13996422bf400063a2c27ad92c3a2e4ce3f92722ec1556f1f1afda2951e4f1c99a683343ed3296fb7a9c69917; shitong_sign=e1c89038");
        Connection connection = Jsoup.connect(zhidao+"python");
        connection.headers(header);
        Document document=connection.get();
        Elements audios = document.select("i[class=i-answer-text]");
        for (Element audio : audios){
            String text=audio.parent().text();
            if(text.startsWith("答：")) {
                if(text.endsWith("...")) {
                    Elements as= audio.parent().parent().select("a[class=ti]");
                    String href=as.first().attr("href");
                    Document answer = Jsoup.connect(href).headers(header).get();
                    Element first=answer.selectFirst("div[class=wgt-best-mask]");
                    if(first==null) {
                        first=answer.selectFirst("div[class=answer-text]");
                        System.out.println(first.text());
                    }
                    else
                    {
                        Element after= first.nextElementSibling();
                        System.out.println(after.text());
                    }

                }
                else {
                    System.out.println(text.substring(2));
                }
            }

        }
    }

}
